﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    public static class LogSystem 
    {
        private static int level = 0;
        private static Dictionary<string, Logger> LoggerMap = new Dictionary<string, Logger>();
        private static LogAppender logAppender = new LogAppender();

        public static bool IsLogAct(LogActType logType)
        {
            return (LogSystem.level & 1 << (int)logType) != 0;
        }

        public static void SetLogAct(LogActType logType,bool act)
        {
            if (act) LogSystem.level |= 1 << (int)logType;
            else LogSystem.level &= ~(1 << (int)logType);
        }

        public static void UnActAllLog()
        {
            LogSystem.level = 0;
        }

        public static Logger GetLogger(string moduleName)
        {
            Logger result;
            lock (LoggerMap)
            {
                Logger logger;
                if (LoggerMap.TryGetValue(moduleName,out logger))
                {
                    result = logger;
                }
                else
                {
                    logger = new Logger(logAppender, moduleName);
                    LoggerMap.Add(moduleName, logger);
                    result = logger;
                }
            }
            return result;
        }

        public static void AddAppender(ILogAppender appender)
        {
            logAppender.Add(appender);
        }

        public static bool RemoveAppender(ILogAppender appender)
        {
            return logAppender.Remove(appender);
        }

        public static void ClearAppenders()
        {
            logAppender.Clear();
        }
    }
}

